package view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.hjc.mvpdemo.MainActivity;
import com.hjc.mvpdemo.R;

import base.BaseActivity;
import base.BasePresenter;
import bean.UserBean;
import presenter.LoginPresenterImpl;

/**
 * Moral :
 * Created by 卖火柴的小女孩 on 2018/2/12.
 */

public class LoginActivity extends BaseActivity<LoginView, LoginPresenterImpl> implements LoginView {

    private Context mContext;
    private EditText etUserName;
    private EditText etUserPwd;
    private Button btnLogin;

    @Override
    protected LoginPresenterImpl createPresenter() {
        return new LoginPresenterImpl(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mContext = this;
        initView();
        initEvent();
    }

    private void initView() {
        etUserName = findViewById(R.id.etUserName);
        etUserPwd = findViewById(R.id.etUserPwd);
        btnLogin = findViewById(R.id.btnLogin);
    }

    private void initEvent() {
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String userName = etUserName.getText().toString();
                String userPwd = etUserPwd.getText().toString();

                if (isEmpty(userName)) {
                    Toast.makeText(mContext, etUserName.getHint().toString(), Toast.LENGTH_SHORT).show();
                    return;
                }
                if (isEmpty(userPwd)) {
                    Toast.makeText(mContext, etUserPwd.getHint().toString(), Toast.LENGTH_SHORT).show();
                    return;
                }

                mPresenter.onLogin(userName,userPwd);
            }
        });
    }


    @Override
    public void onloginSuccess(UserBean userBean) {
        startActivity(new Intent(mContext, MainActivity.class).putExtra(MainActivity.EXTRAUSERBEASN,userBean));
        LoginActivity.this.finish();
    }

    @Override
    public void onloginFail(String message) {
        Toast.makeText(mContext,message,Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.detachView();
    }
}
